package com.easy.util;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.diagnosis.DiagnosisUtils;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.BusinessParams;
import com.alipay.api.request.AlipayTradePrecreateRequest;

import com.alipay.api.FileItem;
import com.easy.bean.Order;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class PaymentUtil {

    public static String Payment(Order order) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();

        // 设置商户订单号
//        model.setOutTradeNo("20150320010101001");
        String no = IdUtil.getSnowflakeNextIdStr();
        model.setOutTradeNo(no);

        // 设置订单总金额
//        model.setTotalAmount("88.88");
        model.setTotalAmount(order.getMoney().toString());

        // 设置订单标题
        model.setSubject("Iphone6 16G");

        // 设置产品码
        model.setProductCode("FACE_TO_FACE_PAYMENT");

        // 设置卖家支付宝用户ID
        model.setSellerId("2088721061035304");

        // 设置订单附加信息
        model.setBody("订单");

        // 设置订单包含的商品列表信息
        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
        GoodsDetail goodsDetail0 = new GoodsDetail();
        goodsDetail0.setGoodsName("ipad");
        goodsDetail0.setQuantity(1L);
        goodsDetail0.setPrice("2000");
        goodsDetail0.setGoodsId("apple-01");
        goodsDetail0.setGoodsCategory("34543238");
        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
        goodsDetail.add(goodsDetail0);
        model.setGoodsDetail(goodsDetail);

//        // 设置业务扩展参数
//        ExtendParams extendParams = new ExtendParams();
//        extendParams.setSysServiceProviderId("2088511833207846");
//        model.setExtendParams(extendParams);

        // 设置商户传入业务信息
        BusinessParams businessParams = new BusinessParams();
        businessParams.setMcCreateTradeIp("127.0.0.1");
        model.setBusinessParams(businessParams);

        // 设置可打折金额
//        model.setDiscountableAmount("80.00");

        // 设置商户门店编号
//        model.setStoreId("NJ_001");

        // 设置商户操作员编号
//        model.setOperatorId("yx_001");

        // 设置商户机具终端编号
//        model.setTerminalId("NJ_T_001");

        // 设置商户的原始订单号
//        model.setMerchantOrderNo("20161008001");

        request.setBizModel(model);
        request.setNotifyUrl("https://6464dcd1.r9.cpolar.top/order/paymentsuccess");//异步回调地址
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            String body = response.getBody();
            JSONObject obj = JSON.parseObject(body);
            obj=obj.getJSONObject("alipay_trade_precreate_response");
            String qr_code = obj.getString("qr_code");
            System.out.println("调用成功");
            return qr_code;
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
             String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
             System.out.println(diagnosisUrl);
             throw new RuntimeException();
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC4BLNth9ImSotjRMfXJ+CnazcrrF2wI3381aScGkhiKZliKf34z4RpIWr1REbNvo9Gk1hM2QS0MVJgNhTtzccepeFnGC70DCZgksDoUKPxnG0XQxBBQ5XQLBwrVtY3yuxX320PdD8W9i3phkHvwWi/gaeyIbb0U0Q4xsrA3w4e2PgZJUa8I/YaxCuquapeIG6xQdxB3OI91WIJ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Q+nNrqYejmilF0xovR85fN3OgjXKubFWWZD6hGsM22bV11q3o+DWzuc6IglapLkqHg/RG7WR3y4jchuejZojb2RUU+Q7iU+APASX8h3NP6YtpWeJOBAoGAFmU7bf8XyTe2lFPlUU0ofokU3ZL6A0BhzTgeZFRszEX09sWYJ5PH1JKxAB6+YzdeFT9dhawQtpXSHekdrfYnvmo5IvqRMmJuLSxnkzdaupvMB5BhNDSPvC8GIpQYxg400wLlo1kbS0RH6gtP89F9lo013mvKEehDrLRWGkckJys=";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr4LjD8wKlzk8hIR+MGi0KJCZFmR6Cb2a/8D6Vpissg5VQ9TKNzsZ5q1F704xUVW5XYfom3V3ahDG6vKEtRHnFVfmaRSlW/0uQt+PpM+FN5HIlg07IiDzsVdzxzOqmHQpb5hZBLRjtgkjVJ3Lzw7SvkH9ieUj801cNWci+/DBv1PR8CWHjPhQVqEidF6ajXQBsV77J7w63WPXIqZbPJxHocvzp/F+v4pvmB0bwtu35opEmsPShfdRA2aNcpGcQcjJ1CZfI8VuvT80aK/2Xm13UC+0Pc7iweB6NzFvejslr7MZEwgfxjqLP4nCLwO1ukocsLCWkmmLgqZ5TLu3yQtCKwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000146600857");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}